﻿<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Asessment.aspx.cs" Inherits="CRMWeb.Pages.Asessment" %>

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
    <title></title>
</head>
<body>


    <div style="float: left; width: 100%">
        <input type="hidden" id="hdnloanid" />
        <input type="hidden" id="hdnbankid1" />
        <input type="hidden" id="hdnbankid2" />
        <input type="hidden" id="hdnAssesmentID" />
        <table style="width: 100%;">
            <tr>
                <td style="vertical-align: top; width: 100%;">
                    <div id="divMsg"></div>
                    <div class="topform_section">
                        <div class="firstform_section">
                            <div id="divLoanTitle" class="ftitle">CREATE LOAN</div>
                            <div class="fitem">
                                <label>Last Result:</label>
                                <input class="easyui-combobox" id="cmblastresult" style="width: 240px;"
                                    data-options="url:'/WebServices/AM/LoanForm.ashx?method=GetAssessmentLastResults',
				                                    valueField:'AssessmentLastResult',
				                                    textField:'AssessmentLastResult',
				                                    panelHeight:'auto'" />
                                <input type="hidden" id="hdnLastRes" runat="server" />
                            </div>
                            <div class="fitem">
                                <label>Last Assessed Date:</label>
                                <input id="cmblastAssessedDate" name="cmblastAssessedDate" class="easyui-datebox" data-options="formatter:au_formatter,parser:au_parser" style="width: 90px;" />

                            </div>
                            <div class="fitem">
                                <label>Account No:</label>
                                <input id="txtAccNo" style="width: 150px;" class="easyui-validatebox" />

                            </div>
                            <div class="fitem">
                                <label>Lender:</label>
                                <input id="txtLender" style="width: 150px;" class="easyui-validatebox" />

                            </div>
                        </div>
                        <div class="firstform_section">
                            <div class="fitem" style="padding-left: 20px; padding-top: 20px;">
                                <label>Note:</label>
                                <textarea id="txtAssesmentNote" style="width: 79%; height: 123px;"></textarea>

                            </div>
                        </div>
                        <div class="firstform_section">
                            <div class="fitem" style="text-align: right;">
                                <%-- <span id="lnkbtnSaveLoan"><a href="javascript:void(0)" id="lnkSaveLoanData" class="easyui-linkbutton" data-options="iconCls:'icon-save'" onclick="SaveLoan()">Save</a></span>
                                <span id="lnkbtnAddLoan"><a href="javascript:void(0)" class="easyui-linkbutton"   data-options="iconCls:'icon-add',disabled:true" onclick="AddLoan()">Create Loan</a></span>
                                <span id="lnkbtnLoan"><a id="abtnLoan" href="javascript:void(0)" class="easyui-linkbutton" data-options="iconCls:'icon-ok'" onclick="btnLoanProfile()">Loan</a></span>--%>
                            </div>
                        </div>
                    </div>
                </td>
            </tr>
        </table>
    </div>

    <script type="text/javascript">

        $(document).ready(function () {
            BindData();
            setHeight();
            $('#ContentPlaceHolder1_appProfile_hdnAssessment').val('true');
            $('#cmblastresult').combobox({
                onLoadSuccess: function () {
                    $('#cmblastresult').combobox('textbox').bind('click', function () {
                        $('#cmblastresult').combobox('showPanel');
                    });
                }

            });

            enableWindowUnload();
        });
        function showPrompt() {
            return 'You have attempted to leave this page. If you have made any changes to the fields without clicking the Save button, your changes will be lost. Are you sure you want to exit this page?';
        }
        function enableWindowUnload() {
            $('#cmblastresult').combobox({
                onSelect: function (n, o) {
                    window.onbeforeunload = showPrompt;
                }
            });

            $('#cmblastAssessedDate').datebox({
                onSelect: function (n, o) {
                    window.onbeforeunload = showPrompt;
                }
            });

            $('#txtAccNo').change(function () {
                window.onbeforeunload = showPrompt;
            });

            $('#txtLender').change(function () {
                window.onbeforeunload = showPrompt;
            });

            $('#txtAssesmentNote').change(function () {
                window.onbeforeunload = showPrompt;
            });

        }


        
        $(document).keydown(function (event) {
            if ((event.which == 83 || event.which == 115) && (event.ctrlKey || event.metaKey) || (event.which == 19)) {
                event.preventDefault();
                SaveAssesmentData();
                return false;
            }
            return true;
        });

        function DateInDBFormat(value) {
            dArr = value.split('/');
            return dArr[2] + '-' + dArr[1] + '-' + dArr[0];
        }

        function formatLoanDate(value, row) {
            if (value == null)
                return value;
            var date = new Date(value);
            var y = date.getFullYear();
            var m = date.getMonth() + 1;
            var d = date.getDate();
            return (d < 10 ? ('0' + d) : d) + '/' + (m < 10 ? ('0' + m) : m) + '/' + y;
        }

        //Australia date format dd/mm/yyyy --From Wade
            function AuDefaultformatLoanDate(value) {
                dArr = value.split('/');
                return dArr[0] + '/' + dArr[1] + '/' + dArr[2];
            }

        function au_formatter(value, row) {
            var date = (value === null) ? new Date() : new Date(value);
            var y = date.getFullYear();
            var m = date.getMonth() + 1;
            var d = date.getDate();
            return (d < 10 ? ('0' + d) : d) + '/' + (m < 10 ? ('0' + m) : m) + '/' + y;
        }

        function au_parser(s) {
            if (!s) return new Date();

            var ss = (s.split('/'));
            var d = new Number(ss[0]);
            var m = new Number(ss[1]);
            var y = new Number(ss[2]);

            if (!isNaN(d) && !isNaN(m) && !isNaN(y)) {
                return new Date(y, m - 1, d);
            }
            else {
                return new Date();
            }
        }
        //End  --20131023  

        // Save Assesment Data when ctrl + S press OR save button clicked
        function SaveAssesmentData() {
           
            window.onbeforeunload = null;
            var assesmentID = $("#hdnAssesmentID").val()
            var accountNo = $("#txtAccNo").val();
            var lender = $("#txtLender").val();
            var LastResult = $('#cmblastresult').combobox('getValue');
            var hOperator = $('#ContentPlaceHolder1_appProfile_hdnUserName').val();
            var assDate = $('#cmblastAssessedDate').datebox('getValue');
            assDate = DateInDBFormat(assDate);
            //insert record into loan table
            $.ajax({
                url: '/WebServices/ApplicationAssesment.ashx?method=saveAssesmentData',
                contentType: 'application/json; charset=utf-8',
                data: {
                    'AssesmentID': assesmentID, 'app_id': getQuerystring('app_id', ''), 'AccountNo': accountNo, 'Lender': lender, 'LastResult': LastResult,
                    'AssesmentDate': assDate, 'Note': $("#txtAssesmentNote").val(), 'hOperator': hOperator
                },
                success: function (data) {
                    $('#hdnAssesmentID').val(data);
                }
            });
        }

        /*Display Assesment Information */
        function BindData() {
            $.ajax({
                url: '/WebServices/ApplicationAssesment.ashx?method=SelectAssesmentData',
                contentType: 'application/json; charset=utf-8',
                data: { 'app_id': getQuerystring('app_id', '') },
                success: function (data) {
                    if (data == "0") {
                        $('#cmblastAssessedDate').datebox('setValue', new Date().getDate() + '/' + (new Date().getMonth() + 1) + '/' + new Date().getFullYear());
                        $('#divLoanTitle').html('Create Loan');
                        $('#hdnAssesmentID').val("0");
                    }
                    else if (data=='1')
                        $.messager.alert('Morgan CRM', '<span style="color:red"> Error while fetching Assesment Detail  </span>');
                    else {
                        $.each(eval(data.replace(/[\r\n]/, '')), function (i, rec) {
                            $('#divLoanTitle').html('Update Loan');
                         
                            if (rec.AssesmentID != null)
                                $('#hdnAssesmentID').val(rec.AssesmentID);
                            else
                                $('#hdnAssesmentID').val("0");

                            $('#txtAccNo').val(rec.accountno);
                            $('#txtLender').val(rec.lender);
                            $("#txtAssesmentNote").val(rec.note);
                            $('#cmblastAssessedDate').datebox('setValue', au_formatter(rec.asseslastdate, null));
                            $('#cmblastresult').combobox('setValue', rec.asseslastresult);

                        });
                    }
                }
            });
        }


    </script>
</body>
</html>
